package com.dengyafei.service.impl;


import com.dengyafei.bean.EnterpriseEntity;
import com.dengyafei.bean.SharesEntity;
import com.dengyafei.bean.SharesEntityExample;
import com.dengyafei.constant.RecommendConstant;
import com.dengyafei.domin.dto.SharesAddDTO;
import com.dengyafei.domin.dto.SharesDeleteDTO;
import com.dengyafei.domin.dto.SharesLikesDTO;
import com.dengyafei.domin.dto.SharesSearchDTO;
import com.dengyafei.domin.vo.SharesAddVO;
import com.dengyafei.domin.vo.SharesDeleteVO;
import com.dengyafei.domin.vo.SharesLikesVO;
import com.dengyafei.domin.vo.SharesSearchVO;
import com.dengyafei.helper.BeanHelper;
import com.dengyafei.mapper.EnterpriseEntityMapper;
import com.dengyafei.mapper.SharesEntityMapper;
import com.dengyafei.service.SharesService;
import com.dengyafei.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 邓亚非
 */
@Service
public class SharesServiceImpl implements SharesService {

    @Autowired
    SharesEntityMapper sharesEntityMapper;

    @Autowired
    HttpServletRequest request;

    @Autowired
    EnterpriseEntityMapper enterpriseEntityMapper;

    @Autowired
    SharesEntityExample sharesEntityExample;
    /**
     * 查询所有的分享
     * @return
     */
    @Override
    public List<SharesEntity> queryAllShares() {
        return sharesEntityMapper.queryAllShares();
    }

    /**
     * 条件查询
     * @param sharesSearchVO
     * @return
     */
    @Override
    public List<SharesEntity> searchByConditon(SharesSearchVO sharesSearchVO) {
        SharesSearchDTO sharesSearchDTO = BeanHelper.copyProperties(sharesSearchVO, SharesSearchDTO.class);
        if(sharesSearchDTO.getSearchType().equals(RecommendConstant.WHOLE)){
            return sharesEntityMapper.queryByWhole(sharesSearchDTO.getSearchContent());
        }
        return sharesEntityMapper.querySharesByCondition(sharesSearchDTO);
    }

    /**
     *  点赞
     * @param sharesLikesVO
     * @return
     */
    @Override
    public Integer addLike(SharesLikesVO sharesLikesVO) {
        SharesLikesDTO sharesLikesDTO = BeanHelper.copyProperties(sharesLikesVO, SharesLikesDTO.class);
        return sharesEntityMapper.updateLike(sharesLikesDTO);
    }

    /**
     * 添加一个分享
     * @param addVO
     * @return
     */
    @Override
    public Integer addShares(SharesAddVO addVO) {
        SharesAddDTO sharesAddDTO = BeanHelper.copyProperties(addVO, SharesAddDTO.class);
        SharesEntity sharesEntity = BeanHelper.copyProperties(sharesAddDTO, SharesEntity.class);
        String realToken = JwtUtil.getRealToken(request);
        String userInfo = JwtUtil.getUserInfo(realToken);
        EnterpriseEntity enterpriseEntity = enterpriseEntityMapper.queryAllByHname(userInfo);
        SharesEntity sharesEntity1 = sharesEntityMapper.queryOne(enterpriseEntity.getHid(), addVO.getContent(), addVO.getTypes());
        if (!ObjectUtils.isEmpty(sharesEntity1)){
            return RecommendConstant.ALREADY_SHARES;
        }
        sharesEntity.setHid(enterpriseEntity.getHid());
        return sharesEntityMapper.insertSelective(sharesEntity);
    }

    /**
     * 删除分享
     * @param sharesDeleteVO
     * @return
     */
    @Override
    public Integer deleteShare(SharesDeleteVO sharesDeleteVO) {
        SharesDeleteDTO sharesDeleteDTO = BeanHelper.copyProperties(sharesDeleteVO, SharesDeleteDTO.class);
        return sharesEntityMapper.deleteByPrimaryKey(sharesDeleteDTO.getSid());
    }

    /**
     * hid查询
     * @return
     */
    @Override
    public List<SharesEntity> queryByHid() {
        String realToken = JwtUtil.getRealToken(request);
        String userInfo = JwtUtil.getUserInfo(realToken);
        EnterpriseEntity enterpriseEntity = enterpriseEntityMapper.queryAllByHname(userInfo);
        return sharesEntityMapper.queryByHid(enterpriseEntity.getHid());
    }
}
